#include <cstdio>
using namespace std;

int CalculateDays(int, int, int);
void AddDaysToYear(int &, int &);
bool IsLeapYear(int);
void ProcessDaysToMonth(int, int);

int month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    int num;
    while (scanf(" %d", &num) != EOF) {
        int year, month, day, diff_days, day_in_year = 0;
        for (int i = 0; i < num; i++) {
            scanf(" %d %d %d %d", &year, &month, &day, &diff_days);
            day_in_year = CalculateDays(year, month, day);
            diff_days = day_in_year + diff_days;
            AddDaysToYear(year, diff_days);
            ProcessDaysToMonth(year, diff_days);
        }
    }

    return 0;
}

int CalculateDays(int year, int month, int day)
{
    if (IsLeapYear(year))  
        month_day[1] = 29;
    else
        month_day[1] = 28;

    int number_days = 0;
    for (int i = 0; i < month - 1; i++)
        number_days += month_day[i];
    return number_days + day;
}

void AddDaysToYear(int &year, int &diff_days)
{
    int total_days_in_year = 0;
    int i;
    for (i = 0; ; i++) {
        if (IsLeapYear(year + i))
            total_days_in_year = 366;
        else
            total_days_in_year = 365;
        if (diff_days <= total_days_in_year)
            break;
        else 
            diff_days -= total_days_in_year;
    }
    year += i;
}

bool IsLeapYear(int year)
{
    if (year % 400 == 0)
        return true;
    if (year % 100 != 0 && year % 4 == 0)
        return true;
    return false;
}

void ProcessDaysToMonth(int year, int days)
{
    if (IsLeapYear(year))
        month_day[1] = 29;
    else
        month_day[1] = 28;

    int i;
    if (days < month_day[0]) {
        i = -1;
    } else {
        for (i = 0; i < 12; i++) {
            days -= month_day[i];
            if (days <= month_day[i + 1])
                break;            
        }
    }
    printf("%04d-%02d-%02d\n", year, i + 2, days);
}

